<?php

require_once ('mailer.php');
require_once ('db.php');
$db = getDb();

$content=file_get_contents("../view/details.html");
$id = $_GET['id'];

$_SUBJECT_="_SUBJECT_";
$_SUBJECT_content="";
$_TO_="_TO_";
$_TO_content="";
$_CC_="_CC_";
$_CC_content="";
$_BCC_="_BCC_";
$_BCC_content="";
$_MESSAGE_="_MESSAGE_";
$_MESSAGE_content="";
$_ATTACHEMENTS_="_ATTACHEMENTS_";
$_ATTACHEMENTS_content="";
$_RESPONSES_="_RESPONSES_";
$_RESPONSES_content="";
$_UNSUBSCRIBERS_="_UNSUBSCRIBERS_";
$_UNSUBSCRIBERS_content="";

getResponsesXpm4();

$sqlCommand="SELECT subject,message FROM subevent WHERE id=". $id;
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	
	$_SUBJECT_content = $result->fields[0];
	$_MESSAGE_content = $result->fields[1];
	
	//TO
	
	$sqlCommand="SELECT e.address,e.name FROM email e JOIN sentsingle ss ON e.id=ss.emailid WHERE ss.subeventid=".$id." AND type=0";
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$_TO_content .= "<option>". $result->fields[0]  . "(". $result->fields[1]  .")</option>\n";
		$result->MoveNext();
	}	
	
	//CC
	
	$sqlCommand="SELECT e.address,e.name FROM email e JOIN sentsingle ss ON e.id=ss.emailid WHERE ss.subeventid=".$id." AND type=1";
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$_CC_content .= "<option>". $result->fields[0]  . "(". $result->fields[1]  .")</option>\n";
		$result->MoveNext();
	}

	// BCC
	
	$sqlCommand="SELECT e.address,e.name FROM email e JOIN sentsingle ss ON e.id=ss.emailid WHERE ss.subeventid=".$id." AND type=2";
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$_BCC_content .= "<option>". $result->fields[0]  . "(". $result->fields[1]  .")</option>\n";
		$result->MoveNext();
	}
	
	// Attachments
	$sqlCommand="SELECT orgFilename, path FROM attachment WHERE orgFilename !=\"\" AND subeventid=" . $id;
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$_ATTACHEMENTS_content .= "<a href=\"". $result->fields[1] . "\">".$result->fields[0]. "</a><br/>\n";
		$result->MoveNext();
	}
	
	// Responses
	$sqlCommand="SELECT e.address,e.name, r.data FROM email e JOIN response r ON e.id = r.emailid WHERE r.subeventid=". $id;
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
		$_RESPONSES_content .= "<li>".  $result->fields[0]  . "(". $result->fields[1]  .")" ."<br/><textarea rows=\"4\" cols=\"50\">".
									$result->fields[2] . "</textarea></li>\n";
		$result->MoveNext();
	}
	
	
	// Unsubs
	$sqlCommand="SELECT e.address,e.name FROM email e JOIN unsub u ON e.id = u.emailid JOIN subevent s ON u.eventid = s.eventid WHERE s.id=". $id;
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	while (!$result->EOF)
	{
  
		$_UNSUBSCRIBERS_content .= "<li>".  $result->fields[0]  . "(". $result->fields[1]  .")" ."</li><br/>\n";
		$result->MoveNext();
	}
	
	

 $content = str_replace($_TO_ , $_TO_content, $content);
 $content = str_replace($_CC_ , $_CC_content, $content);
 $content = str_replace($_BCC_ , $_BCC_content, $content);
 $content = str_replace($_MESSAGE_ , $_MESSAGE_content, $content);
 $content = str_replace($_ATTACHEMENTS_ , $_ATTACHEMENTS_content, $content);
 $content = str_replace($_SUBJECT_ , $_SUBJECT_content, $content);
 $content = str_replace($_RESPONSES_ , $_RESPONSES_content, $content);
 $content = str_replace($_UNSUBSCRIBERS_ , $_UNSUBSCRIBERS_content, $content);
 
 
 // Display page
 echo $content;
 
?>
